package neu.soft.facade;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import neu.soft.entity.OperationPurchaseOrder;
import neu.soft.exception.AppException;
import neu.soft.exception.AppExceptionCodeMsg;
import neu.soft.vendormock.service.SupplyMockService;
import neu.soft.vo.MsgBean;
import neu.soft.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author 李博
 * 购货统一门面
 */
@Component
@Slf4j
public class PurchaseComprehensiveFacade {

    @Autowired
    PurchaseOrderMessageHandler messageHandlerFacade;

    @Autowired
    SupplyMockService supplyMockService;

    @Autowired
    MessageSender messageSendFacade;

    //接收业务中心购货单
    @SneakyThrows
    public void receivePurchaseOrder(OperationPurchaseOrder operationPurchaseOrder) {
        Integer financialOrderId = messageHandlerFacade.receivePurchaseOrder(operationPurchaseOrder);
        if (financialOrderId == null) {
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }
        messageSendFacade.sendPurchaseOrder(financialOrderId);
        Map<String,Object> map = messageHandlerFacade.sendVendorInfo(financialOrderId);
        Map<String,Object> res = supplyMockService.invoiceMock(map);//模拟发送给供应商
        //往前端发消息

        WebSocketServer.sendInfo(new MsgBean(res,"您有新的采购发票待处理"),"boyan");//有异常处理

    }

    //生成应付单
    public void generatePayableOrder(Integer operationOrderId) {
        if (operationOrderId == null) {
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }
        messageHandlerFacade.generatePayableOrder(operationOrderId);
    }




}
